package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.NewActivityRuleService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.NewActivityPlanDao;
import com.qdlc.p2p.dal.dao.NewActivityRuleDao;
import com.qdlc.p2p.dal.dto.NewActivityPlan;
import com.qdlc.p2p.dal.dto.NewActivityRule;
import com.qdlc.p2p.dal.model.NewActivityRuleModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Service("newActivityRuleService")
public class NewActivityRuleServiceImpl implements NewActivityRuleService {

    @Resource
    private NewActivityRuleDao newActivityRuleDao;
    @Resource
    private NewActivityPlanDao newActivityPlanDao;

    /**
     * 通过活动id进行查询
     *
     * @param activityId
     * @return
     */
    @Override
    public List<NewActivityRule> findByActivityId(long activityId) {
        return newActivityRuleDao.findByActivityId(activityId);
    }

    /**
     * 后台活动规则列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<NewActivityRuleModel> list(NewActivityRuleModel model) {
        return newActivityRuleDao.list(model);
    }

    /**
     * 根据活动规则id进行查询
     *
     * @param id
     * @return
     */
    @Override
    public NewActivityRuleModel findByActivityRuleId(long id) {
        return newActivityRuleDao.findByActivityRuleId(id);
    }

    /**
     * 保存活动规则
     *
     * @param model
     * @return
     */
    @Override
    public NewActivityRule save(NewActivityRuleModel model) {
        NewActivityRule newActivityRule = inverseModel(model);
        newActivityRule.setCreator(model.getCreator());
        newActivityRule.setCreateTime(model.getCreateTime());
        return newActivityRuleDao.save(newActivityRule);
    }

    /**
     * 更新活动规则
     *
     * @param model
     * @return
     */
    @Override
    public NewActivityRule update(NewActivityRuleModel model) {
        NewActivityRule newActivityRule = inverseModel(model);
        //设置更新域
        newActivityRule.setId(model.getId());
        newActivityRule.setModifier(model.getModifier());
        newActivityRule.setModifyTime(model.getModifyTime());
        NewActivityRule searchRule = newActivityRuleDao.find(model.getId());
        if (null == searchRule) return null;
        newActivityRule.setCreator(searchRule.getCreator());
        newActivityRule.setCreateTime(searchRule.getCreateTime());
        return newActivityRuleDao.update(newActivityRule);
    }

    /**
     * 设置开启关闭活动规则
     *
     * @param model
     * @return
     */
    @Override
    public int toggleStatus(NewActivityRuleModel model) {
        return newActivityRuleDao.toggleStatus(model);
    }


    /**
     * 将导出类转成基类
     * 这里只设置save和update公共域
     *
     * @param model
     * @return
     */
    public NewActivityRule inverseModel(NewActivityRuleModel model) {
        NewActivityRule newActivityRule = new NewActivityRule();
        NewActivityPlan newActivityPlan = newActivityPlanDao.find(model.getActivityId());
        if (null == newActivityPlan) return null;
        newActivityRule.setNewActivityPlan(newActivityPlan);
        newActivityRule.setRuleId(model.getRuleId());
        newActivityRule.setStatus(model.getStatus());
        newActivityRule.setType(model.getType());
        newActivityRule.setStartTime(model.getStartTime());
        newActivityRule.setEndTime(model.getEndTime());
        newActivityRule.setDescription(model.getDescription());
        //通过规则类型设置活动规则的业务bean名称
        switch (model.getType()) {
            case (1):
                newActivityRule.setBusinessClass("giveUserRedEnvelopeService");
                break;
            case (2):
                newActivityRule.setBusinessClass("giveUserExperienceService");
                break;
            case (3):
                newActivityRule.setBusinessClass("giveUserUpRateService");
                break;
            case (4):
                newActivityRule.setBusinessClass("giveUserLotteryService");
                break;
            case (5):
                newActivityRule.setBusinessClass("giveUserMovieTicketService");
                break;
            case (7):
                newActivityRule.setBusinessClass("giveUserWineCouponsService");
                break;
        }
        return newActivityRule;
    }
}
